package com.hndfsj.minio.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class MinioTempCleanService {
    private static final Logger LOGGER = LoggerFactory.getLogger(MinioTempCleanService.class);

    public MinioTempCleanService() {
    }

    public void cleanTemp(String tmpDir, Long tmpAliveDuration) {
        if (tmpAliveDuration != null && tmpAliveDuration > 0L) {
            LOGGER.info("Clean temp dir {}, duration: {}", tmpDir, tmpAliveDuration);
            File dir = new File(tmpDir);
            if (dir.exists() && dir.isDirectory()) {
                File[] files = dir.listFiles();
                if (files == null) {
                    return;
                }

                long currentTime = System.currentTimeMillis();
                File[] var7 = files;
                int var8 = files.length;

                for(int var9 = 0; var9 < var8; ++var9) {
                    File file = var7[var9];
                    long lastModified = file.lastModified();
                    if (file.canWrite() && currentTime - lastModified >= tmpAliveDuration) {
                        file.delete();
                    }
                }
            }

        } else {
            LOGGER.warn("Temp dir is not cleared, due to temp files alive duration: {}", tmpAliveDuration);
        }
    }
}